package mi.payment.ui.print.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.List;

import mi.payment.R;
import mi.payment.adapter.BaseListAdapter;
import mi.payment.constants.TransShortType;
import mi.payment.db.dao.PayDetail;
import mi.payment.utils.CollectionUtil;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.NumberUtil;

/**
 * @author longx on 2017/5/5.
 */

public class DetailListView extends LinearLayout {

    private static final int LINER_SHOW_MAX_LINE = 500;//LinearLayout ADD View 最大的行数，大于这个行数用listview来显示

    private ListView listView;
    private LinearLayout linearLayoutList;
    private ScrollView scvPrintDetailList;

    public DetailListView(Context context) {
        super(context);
        initView();
    }

    public DetailListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        View view = View.inflate(getContext(), R.layout.layout_detail_list_view, this);
        listView = (ListView) view.findViewById(R.id.list);
        linearLayoutList = (LinearLayout) view.findViewById(R.id.lin_list);
        scvPrintDetailList = (ScrollView) view.findViewById(R.id.scv_print_detail_list);
    }

    public void fillData(List<PayDetail> list) {
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        if (list.size() > LINER_SHOW_MAX_LINE) {
            fillData4ListView(list);
        } else {
            fillData4Liner(list);
        }
    }

    private void fillData4Liner(List<PayDetail> list) {
        listView.setVisibility(GONE);
        scvPrintDetailList.setVisibility(VISIBLE);
        linearLayoutList.setVisibility(VISIBLE);
        for (int i = 0; i < list.size(); i++) {
            View itemLayout = View.inflate(getContext(), R.layout.item_transcation_detail, null);
            TextView amt = (TextView) itemLayout.findViewById(R.id.amt);
            TextView auth = (TextView) itemLayout.findViewById(R.id.auth);
            TextView card = (TextView) itemLayout.findViewById(R.id.card);
            TextView type = (TextView) itemLayout.findViewById(R.id.type);
            TextView voucher = (TextView) itemLayout.findViewById(R.id.voucher);

            PayDetail item = list.get(i);
            amt.setText(MoneyUtils.longCent2DoubleMoneyStr(item.amount));
            auth.setText(item.authNo);
            auth.setVisibility(View.GONE);
            card.setText(NumberUtil.intercept(item.CardNo));
            String trType = "";
            if (item.getTransPlatform() == 0) {
                trType = item.transType;
            } else {
                trType = item.getTransPlatform() + "+" + item.getTransactionType();
            }
            type.setText(TransShortType.type.get(trType));
            voucher.setText(item.voucherNo);
            linearLayoutList.addView(itemLayout);
        }
    }

    private void fillData4ListView(List<PayDetail> list) {
        scvPrintDetailList.setVisibility(GONE);
        linearLayoutList.setVisibility(GONE);
        listView.setVisibility(VISIBLE);
        DetailAdapter adapter = new DetailAdapter(getContext());
        adapter.setData(list);
        listView.setAdapter(adapter);
    }

    private static class DetailAdapter extends BaseListAdapter<PayDetail> {

        private DetailAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = View.inflate(getContext(), R.layout.item_transcation_detail, null);
                viewHolder.amt = (TextView) convertView.findViewById(R.id.amt);
                viewHolder.auth = (TextView) convertView.findViewById(R.id.auth);
                viewHolder.card = (TextView) convertView.findViewById(R.id.card);
                viewHolder.type = (TextView) convertView.findViewById(R.id.type);
                viewHolder.voucher = (TextView) convertView.findViewById(R.id.voucher);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            PayDetail item = getItem(position);

            viewHolder.amt.setText(MoneyUtils.longCent2DoubleMoneyStr(item.amount));
            viewHolder.auth.setText(item.authNo);
            viewHolder.auth.setVisibility(View.GONE);
            viewHolder.card.setText(NumberUtil.intercept(item.CardNo));
            String trType = "";
            if (item.getTransPlatform() == 0) {
                trType = item.transType;
            } else {
                trType = item.getTransPlatform() + "+" + item.getTransactionType();
            }
            viewHolder.type.setText(TransShortType.type.get(trType));
            viewHolder.voucher.setText(item.voucherNo);
            return convertView;
        }

        private static class ViewHolder {
            private TextView amt;
            private TextView auth;
            private TextView card;
            private TextView type;
            private TextView voucher;
        }
    }

}
